package com.youlai.system.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.youlai.system.common.result.Result;
import com.youlai.system.common.util.ExcelUtils;
import com.youlai.system.model.entity.Employ;
import com.youlai.system.model.form.EmployForm;
import com.youlai.system.model.query.EmployQuery;
import com.youlai.system.model.vo.*;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.plugin.easyexcel.EmployImportListener;
import com.youlai.system.service.DepartmentService;
import com.youlai.system.service.EmployService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.poi.hpsf.Array;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@Tag(name = "09.我的员工接口")
@RestController
@RequestMapping("/api/v1/mydEmploy")
@RequiredArgsConstructor
public class EmployController {

    private final EmployService employService;
    private final DepartmentService departmentService;

    @Operation(summary = "获取当前登录员工信息")
    @GetMapping("/me")
    public Result<Employ> getCurrentEmployInfo() {
        Employ employ = employService.getCurrentEmployInfo();
        return Result.success(employ);
    }

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    public Result<PageInfo<EmployVO>> getEmployList(
            @ParameterObject EmployQuery queryParams
    ) {
        PageInfo<EmployVO> list= employService.getEmployList(queryParams);
        System.out.println(list.getList());
        return Result.success(list);
    }

    @Operation(summary = "新增员工")
    @PostMapping("/save")
    @PreventDuplicateSubmit
    public Result saveEmploy(
            @Valid @RequestBody EmployForm employForm
    ) {
        Integer id = employService.saveEmploy(employForm);
        System.out.println(id);
        return Result.success(id);
    }

    @Operation(summary = "修改员工")
    @PostMapping("/update")
    @PreventDuplicateSubmit
    public Result updateEmploy(
            @Valid @RequestBody EmployForm employForm
    ) {
        Integer id = employService.UpdateEmploy(employForm);
        return Result.success(id);
    }

    @Operation(summary = "删除员工")
    @DeleteMapping("/{ids}")
    public Result deleteDepartments(
            @Parameter(description ="员工ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = employService.deleteByIds(ids);
        return Result.judge(result);
    }
    @Operation(summary = "删除员工")
    @PostMapping("/{id}/updatefalse")
    public Result deleteEmploys(
            @PathVariable("id") Integer id
    ) {
        boolean result = employService.deleteEmploy(id);
        return Result.judge(result);
    }

    @Operation(summary = "获取员工基本信息")
    @GetMapping("/{id}/form")
    public Result<EmployForm> getRoleForm(
            @Parameter(description = "员工id") @PathVariable Long id
    ) {
        EmployForm employForm = employService.getEmployForm(id);
        return Result.success(employForm);
    }

    @Operation(summary = "分配员工角色")
    @PostMapping("/saveRole")
    @PreventDuplicateSubmit
    public Result saveEmployRole(
          Integer id,@RequestBody String[] roleIdArray
    ) {
        System.out.println(Arrays.toString(roleIdArray));
        Integer id1 = employService.saveEmployRole(id,roleIdArray);
        return Result.success(id1);
    }

    @Operation(summary = "分配员工角色1")
    @PostMapping("/saveRole1")
    public Result saveEmployRole1(
            Integer id,String roleIdArray
    ) {
//        System.out.println(Arrays.toString(roleIdArray));
        Integer id1 = employService.saveEmployRole1(id,roleIdArray);
        return Result.success(id1);
    }

    @Operation(summary = "员工导入模板下载")
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        String fileName = "员工导入模版.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

        String fileClassPath = "excel-templates" + File.separator + fileName;
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileClassPath);

        ServletOutputStream outputStream = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();

        excelWriter.finish();
    }

    @Operation(summary = "导入员工")
    @PostMapping("/_import")
    public Result importEmploys(MultipartFile file) throws IOException {

        EmployImportListener listener = new EmployImportListener();
        String msg = ExcelUtils.importExcel(file.getInputStream(), EmployImportVO.class, listener);
        return Result.success(msg);
    }

    @Operation(summary = "导出员工")
    @GetMapping("/_export")
    public void exportUsers(EmployQuery queryParams, HttpServletResponse response) throws IOException {
        String fileName = "员工列表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

        List<EmployExportVO> exportEmployList = employService.listExportEmploy(queryParams);
        System.out.println(exportEmployList);
        EasyExcel.write(response.getOutputStream(), EmployExportVO.class).sheet("用户列表")
                .doWrite(exportEmployList);
    }


    @Operation(summary = "保存员工头像")
    @PostMapping("/savePhoto")
    @PreventDuplicateSubmit
    public Result saveEmployPhoto( String staffPhoto,Integer id) {
        Integer Id = employService.saveEmployPhoto(staffPhoto,id);
        return Result.success(Id);
    }

}
